package br.com.spb.support;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletResponse;

 
public class FileUtil {

	public static byte[] read(File file) throws IOException {

		byte[] content = null;
		int fileLength = (int) file.length();
		FileInputStream fileInput = null; 
		
		try { 
			fileInput = new FileInputStream(file);
 
			BufferedInputStream bufferedInput = new BufferedInputStream(fileInput);
			content = new byte[fileLength];
			bufferedInput.read(content, 0, fileLength);
			bufferedInput.close();
		} finally {
			if (fileInput != null) {
				fileInput.close();
			}
			System.out.print("#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-# \n " +
					         "#-#-#-                   erro de upload do arquivo           -#-#-#-#-#-# \n " + 
					         "#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#");
		}

		return content;
	}

	public static void download(String filename, byte[] content, HttpServletResponse response) throws IOException {
		if (filename.substring(filename.lastIndexOf(".") + 1).equalsIgnoreCase("PDF")) {
			response.setContentType("application/pdf");
		} else if (filename.substring(filename.lastIndexOf(".") + 1).equalsIgnoreCase("JPG")) {
			response.setContentType("image/jpeg");
		} else if (filename.substring(filename.lastIndexOf(".") + 1).equalsIgnoreCase("PNG")) {
			response.setContentType("image/x-png");
		} else if (filename.substring(filename.lastIndexOf(".") + 1).equalsIgnoreCase("GIF")) {
			response.setContentType("image/gif");
		} else if (filename.substring(filename.lastIndexOf(".") + 1).equalsIgnoreCase("BMP")) {
			response.setContentType("image/x-ms-bmp");
		}else if (filename.substring(filename.lastIndexOf(".") + 1).equalsIgnoreCase("XLS")) {
			response.setContentType("application/vnd.ms-excel"); 
		}else if (filename.substring(filename.lastIndexOf(".") + 1).equalsIgnoreCase("XLSX")) {
			response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); 
		}else if(filename.substring(filename.lastIndexOf(".") + 1).equalsIgnoreCase("DOCX")) {
			response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document"); 
		}else if(filename.substring(filename.lastIndexOf(".") + 1).equalsIgnoreCase("DOC")) {
			response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document"); 
		}else if(filename.substring(filename.lastIndexOf(".") + 1).equalsIgnoreCase("CSV")) {
			response.setContentType("application/vnd.ms-excel"); 
	    } 

		response.addHeader("Content-Disposition", "attachment; filename=" + filename);

		OutputStream out = response.getOutputStream();
		InputStream inputStream = new ByteArrayInputStream(content);

		byte[] buf = new byte[content.length];
		int count;
		while ((count = inputStream.read(buf)) >= 0) {
			out.write(buf, 0, count);
		}
		inputStream.close();
		out.flush();
		out.close();

		response.getOutputStream().flush();
	}
 

	/**
	 * @param filename
	 * @param xls
	 * @param response
	 * @throws IOException
	 */
	public static void downloadCSV(HttpServletResponse response,String csv, String nome) throws IOException {  
		
		response.setHeader("Cache-Control", "no-cache"); 
		response.setHeader("Cache-Control", "max-age=0");
		response.setCharacterEncoding("ISO-8859-1");
		response.setContentType("application/vnd.ms-excel;charset=UTF-8");  
  
        response.setHeader("Content-Disposition","attachment; filename ="+nome);
		OutputStream out = response.getOutputStream();
		InputStream inputStream = new ByteArrayInputStream(csv.getBytes("ISO-8859-1"));

		byte[] buf = new byte[csv.getBytes().length];
		int count;
		while ((count = inputStream.read(buf)) >= 0) {
			out.write(buf, 0, count);
		}
		inputStream.close();
		out.flush();
		out.close();

		response.getOutputStream().flush();
	}
}
